/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: 沈小白 2020-07-22 $
 */
import store from '@/store'
module.exports={
	//检测是否登录
	islogin:function(){
		/*获取上页面的路径*/ 
		let pageArr=getCurrentPages();
		let pageData;
		if(pageArr.length>1){
			pageData=getCurrentPages()[1].__page__;
		}else{
			pageData=getCurrentPages()[0].__page__;
		}
		store.dispatch('setPrePage',pageData)
		if(!store.state.hasLogin){
			//除了h5都跳转
			//#ifdef MP
			uni.reLaunch({
				url:"/pages/user/get_user_info/get_user_info"
			})
			//#endif
			//h5跳转
			//#ifndef MP
			
			uni.redirectTo({
				url:"/pages/user/login/login"
			})
			
			//#endif
		}
	},
	//弹窗
	/*icon传参数 showToast的参数
	time: number类型
	*/
	toast:function(icon,title,time,isBlack){
		uni.showToast({
			icon:icon,
		    title: title,
		    duration: time,
			
		});
		setTimeout(function () {
		   if(isBlack){
		   	uni.navigateBack({
		   		delta:1
		   	})
		   }
		}, time);
		
	},
	model:function(content,isBack){
		uni.showModal({
		    title:"",
		    content: content,
			showCancel:false,
		    success: function (res) {
		        if (res.confirm) {
		            console.log('用户点击确定');
					if(isBack==true){
						uni.navigateBack({
							delta:1
						})
					}
		        } else if (res.cancel) {
		            console.log('用户点击取消');
		        }
		    }
		});
	},
	//路径回退 num number类型  1
	jump:function(num){
		let pages=getCurrentPages()
		let router=pages[0].route
		console.log(router)
		if(router=="pages/user/login/login"||router=="/pages/user/reg/reg"||
			router=="pages/user/login/validation/validation"
			||router=="pages/user/login/setLoginPwd/setLoginPwd"
			||router=="pages/user/login/bindLogin/bindLogin"
			){
			uni.switchTab({
				url:"/pages/index/index"
			})
		}else if(router!="pages/user/login/login"){
			console.log("执行");
			console.log(router)
			console.log(store)
			let prePage=store.state.prePage;
			let url=prePage.path;
			if(prePage.options){
				let arr= new Array()
				for(let item in prePage.options){
					console.log(item);
					let options = new String();
					options=item + '=' + prePage.options[item];
					arr.push(options)
					
				}
				/*页面跳转*/ 
				if(arr){
					Array.from(arr,item=>{
						item='&'+item
						url=url+'?'+item
					})
				}
			}
			console.log(url);
			uni.reLaunch({
				url:url
			})
		}
		
	},
	Obj2Str:function(obj) {
	    var str = '';
	    for (var i in obj) {
	        str += (str ? '&' : '') + i + '=' + obj[i]
	    }
	    return str;
	},
	/** 格式化时间 */
	format:function(timestamp, fmt) {
	    var date = new Date();
	    date.setTime(timestamp * 1000);
	    var o = {
	        "M+": date.getMonth() + 1, //月份 
	        "d+": date.getDate(), //日 
	        "h+": date.getHours(), //小时 
	        "m+": date.getMinutes(), //分 
	        "s+": date.getSeconds(), //秒 
	        "q+": Math.floor((date.getMonth() + 3) / 3), //季度 
	        "S": date.getMilliseconds() //毫秒 
	    };
	    if (typeof fmt == 'undefined') fmt = 'yyyy-MM-dd hh:mm:ss';
	    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
	    for (var k in o)
	        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	    return fmt;
	},
	//验证密码  
	// 对应的验证规则是：密码中必须包含字母、数字、特称字符，至少6个字符，最多16个字符。 
	VerifyPassword(password){
		var regex = new RegExp('^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$'); //符合格式的
		var num =new RegExp('^[0-9]{6,16}$') //验证数字
		if(num.test(password)==true){
			this.toast("none","密码不能全是数字",3000)
			return false
		}
		if(regex.test(password)==true){
			return regex.test(password)
		}else{
			this.toast("none","密码过于简单",3000)
			return false;
			
		}
		
	},
	remainTime(diffms) { //倒计时
		const daysms = 24 * 60 * 60 * 1000;
		const hoursms = 60 * 60 * 1000;
		const minutems = 60 * 1000;
		const secondms = 1000;
		var result = '';
		var hasfrontVal = false;
	
		var differDay = Math.floor(diffms / daysms);
		result = differDay ? differDay + '天' : '0天';
		hasfrontVal = differDay ? true : false;
	
		diffms -= differDay * daysms;
		var differHour = Math.floor(diffms / hoursms);
		result += differHour ? differHour + '时' : '0时';
		hasfrontVal = differHour ? true : false;
	
		diffms -= differHour * hoursms;
		var differMinute = Math.floor(diffms / minutems);
		result += differMinute ? differMinute + '分' : '0分';
	
		diffms -= differMinute * minutems;
		var differSecond = Math.floor(diffms / secondms);
		result += differSecond ? differSecond + '秒' : '0秒';
	
		return result;
	}

	
	
}